import {ecs} from "db://oops-framework/libs/ecs/ECS";
import {StartMjDeskComp} from "db://assets/script/game/majiang/comp/startMjDeskComp";
import {smc} from "db://assets/script/game/common/singletonModuleComp";
import {MjDeskView} from "db://assets/script/game/majiang/view/mjDeskView";
import {Player} from "db://assets/script/game/player/player";

// 游戏开始
export class StartMjDeskSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(StartMjDeskComp)
    }

    entityEnter(entity: ecs.Entity) {
        if (entity.has(StartMjDeskComp)) {
            console.log("mj desk start")
            const startComp = entity.get(StartMjDeskComp)
            // 更新牌
            // 本家
            const comp = smc.gameMj.get(MjDeskView)
            const playerView = comp.getPlayerView()
            // 隐藏规则牌
            comp.hideOptShow()
            for (const k of Object.keys(smc.gameMj.playerCards)) {
                const shortId = Number(k)
                // 清除旧牌
                playerView.resetCardsByShortId(shortId)
                // 更新新牌
                playerView.addCardByShortId(shortId, smc.gameMj.playerCards[k])
                if (Player.isSelf(shortId)) {
                    if (startComp.newTile) {
                        // 轮到本家出牌，显示刚拿到的牌
                        playerView.addNewTile(shortId, startComp.newTile)
                    }
                    // 本家, 更新牌信息,监听牌移动
                    playerView.updateCardPositionByShortId(shortId)
                    playerView.listenPlayerCardByShortId(shortId)
                }
                // 把准备好的手势去掉
                playerView.toggleReadyHandByShortId(shortId, false)
                // 显示当前出牌的人
                if (smc.gameMj.playerInfos[k].playerDeskInfo.pos === smc.gameMj.roundInfo.curTurn) {
                    comp.startCounterByShortId(shortId)
                }
            }
            entity.remove(StartMjDeskComp)
        }
    }

}
